<template>
  <div>
    <el-card>
      <el-page-header icon="" title="用户管理">
        <template #content>
          <span class="text-large font-600 mr-3" style="font-size: 16px">
            用户列表
          </span>
        </template>
      </el-page-header>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="user_name" label="用户名" width="180" />
        <el-table-column label="头像" width="180">
          <template #default="scope">
            <div v-if="scope.row.avatar">
              <el-avatar
                :size="50"
                :src="'http://localhost:3000' + scope.row.avatar"
              ></el-avatar>
            </div>
            <div v-else>
              <el-avatar
                :size="50"
                src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="角色">
          <template #default="scope">
            <!-- {{ scope.row.is_admin === true ? "管理员" : "编辑" }} -->
            <el-tag v-if="scope.row.is_admin === true" type="danger"
              >管理员</el-tag
            >
            <el-tag v-else type="success">编辑</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>

            <el-popconfirm
              title="你确定删除吗"
              cancel-button-text="取消"
              confirm-button-text="确定"
              @confirm="handleDelete(scope.row)"
            >
              <template #reference>
                <el-button size="small" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        v-model="dialogVisible"
        title="编辑用户"
        width="500"
        :before-close="handleClose"
      >
        <el-form
          ref="userFormRef"
          style="max-width: 600px"
          :model="userForm"
          :rules="userFormRules"
          label-width="auto"
          class="demo-ruleForm"
          status-icon
        >
          <el-form-item label="用户名" prop="user_name">
            <el-input v-model="userForm.user_name" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="userForm.password" type="password" />
          </el-form-item>
          <el-form-item label="角色" prop="is_admin">
            <el-select
              v-model="userForm.is_admin"
              placeholder="Select"
              style="width: 100%"
            >
              <el-option label="管理员" :value="true"></el-option>
              <el-option label="编辑" :value="false"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="个人简介" prop="introduction">
            <el-input v-model="userForm.introduction" type="textarea" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleConfirm()">
              确认
            </el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import axios from "axios";

const dialogVisible = ref(false);
const tableData = ref([]);
const userFormRef = ref();

const userForm = reactive({
  user_name: "",
  password: "",
  is_admin: false, //编辑
  introduction: "",
});

const userFormRules = reactive({
  user_name: [{ required: true, message: "请输入名字", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  introduction: [
    { required: true, message: "请输入个人介绍", trigger: "blur" },
  ],
  is_admin: [{ required: true, message: "请选择权限", trigger: "blur" }],
});

onMounted(() => {
  getTableData();
});
const getTableData = async () => {
  const res = await axios.get("/adminapi/user/list");
  // console.log(res.data);
  tableData.value = res.data.result.list;
};
//编辑回调
const handleEdit = async (data) => {
  // console.log(data);
  const res = await axios.get(`/adminapi/user/list/${data.id}`);
  Object.assign(userForm, res.data.result);
  dialogVisible.value = true;
};
const handleDelete = async (data) => {
  await axios.delete(`/adminapi/user/list/${data.id}`);
  //reload data
  getTableData();
  //tabledata 本地删除 性能最好
};
//编辑确认回调
const handleConfirm = () => {
  userFormRef.value.validate(async (valid) => {
    if (valid) {
      //更新后端
      await axios.put(`/adminapi/user/list/${userForm.id}`, userForm);
      // 弹出框隐藏
      dialogVisible.value = false;
      // 获取table数据
      getTableData();
    }
  });
};

//reload page
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 50px;
}
</style>
